package site.marqstree.kotlin.usercenter.ui.activity.userinfo

import android.os.Bundle
import kotlinx.android.synthetic.main.activity_user_info.*
import org.jetbrains.anko.toast
import site.marqstree.kotlin.library.base.extent.onClick
import site.marqstree.kotlin.library.base.util.GlideUtil
import site.marqstree.kotlin.usercenter.R
import site.marqstree.kotlin.library.base.data.bean.user.UserInfo
import site.marqstree.kotlin.library.base.ui.activity.BaseTakePhotoMvpActivity
import site.marqstree.kotlin.usercenter.injection.component.DaggerUserComponent
import site.marqstree.kotlin.library.base.util.UserUtil
import site.marqstree.kotlin.library.base.util.qiniu.IQiniu


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.usercenter.ui.activity
 * 文件名: UserInfoActivity
 * 创建者: marqstree
 * 创建时间: 2020/2/23 9:11
 * 描述: TODO
 */

/*
    用户信息
 */
class UserInfoActivity: BaseTakePhotoMvpActivity<UserInfoPresenter>(),
    UserInfoView {

    private var mUserIcon:String = UserUtil.getUserIcon()
    private var mUserName:String = UserUtil.getUserName()
    private var mUserMobile:String = UserUtil.getUserMobile()
    private var mUserGender:String = UserUtil.getUserGender()
    private var mUserSign:String = UserUtil.getUserSign()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_info)

        loadDataRefreshUI()
        initView()
    }

    /*
        初始化视图
     */
    private fun initView() {
        mUserIconView.onClick {
            takePhoto2Qiniu(object :IQiniu{
                override fun success(remoteFileUrl: String) {
                    GlideUtil.loadImage(this@UserInfoActivity,
                                        remoteFileUrl,
                                        mUserIconIv)
                }
            })
        }

        mHeaderBar.getRightView()
            .onClick {
                //保存用户信息
                mPresenter.editUser(UserUtil.getUserId(),
                    UserUtil.getUserIcon(),
                    mUserNameEt.text?.toString()?:"",
                    if(mGenderMaleRb.isChecked) "male" else "female",
                    mUserSignEt.text?.toString()?:""
                )
            }
    }

    /*
        初始化界面
        从sp中取出用户数据
        刷新UI
     */
    private fun loadDataRefreshUI() {
        mUserIcon = UserUtil.getUserIcon()
        mUserName = UserUtil.getUserName()
        mUserMobile = UserUtil.getUserMobile()
        mUserGender = UserUtil.getUserGender()
        mUserSign = UserUtil.getUserSign()

        //mRemoteFileUrl = mUserIcon
        if (mUserIcon != ""){
            //设置图片
            GlideUtil.loadImage(this,
                mUserIcon!!,
                mUserIconIv)
        }else{
            mUserIconIv.setImageResource(R.drawable.icon_default_user)
        }
        mUserNameEt.setText(mUserName)
        mUserMobileTv.text = mUserMobile

        if (mUserGender == "male") {
            mGenderMaleRb.isChecked = true
        }
        else {
            mGenderFemaleRb.isChecked = true
        }

        mUserSignEt.setText(mUserSign)

    }

    //业务模块级依赖注入
    override fun initBusinessInjection() {
        DaggerUserComponent
            .builder()
            .activityComponent(activityComponent)
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    override fun takePhotoResult(remoteFileUrl: String) {
        GlideUtil.loadImage(this@UserInfoActivity,
            remoteFileUrl,
            mUserIconIv)
    }

    /*
        编辑用户资料回调
     */
    override fun onEditUserResult(result: UserInfo) {
        toast("修改成功")
        UserUtil.putUserInfo(result)
        loadDataRefreshUI()
    }
}